<?php
namespace app\ebcms\controller;
class Manager extends \app\ebcms\controller\Common
{
    
    public function index()
    {
        if (request()->isGet()) {
            return $this->fetch();
        }
    }

    public function add()
    {
        if (request()->isGet()) {
            return \ebcms\Form::fetch();
        } elseif (request()->isPost()) {
            $obj = new \app\ebcms\model\Manager();
            if (false !== $obj ->validate('Manager.add')->save(input())) {
                $this -> success('操作成功！');
            }else{
                $this -> error($obj -> getError());
            }
        }
    }

    public function edit()
    {
        if (request()->isGet()) {
            $obj = \app\ebcms\model\Manager::get(input('id'));
            return \ebcms\Form::fetch($obj);
        } elseif (request()->isPost()) {
            $obj = \app\ebcms\model\Manager::get(input('id'));
            $allowfield = ['nickname', 'avatar'];
            if ($obj['email'] !== \think\Config::get('super_admin')) {
                $allowfield = ['nickname', 'email', 'avatar'];
            }
            if (false !== $obj -> validate('Manager.edit') -> allowfield($allowfield) -> isUpdate(true) -> save(input())) {
                $this -> success('操作成功！');
            }else{
                $this -> error($obj -> getError());
            }
        }
    }

    public function status($id)
    {
        $obj = \app\ebcms\model\Manager::get(input('id'));
        if ($obj['email'] == \think\Config::get('super_admin')) {
            $this -> error('超级管理员不支持此操作！');
        }
        
        $obj -> status = input('value')?1:0;
        $obj -> save();
        $this -> success('操作成功！');
    }

    public function delete($id)
    {
        $obj = \app\ebcms\model\Manager::get(input('id'));
        if ($obj['email'] == \think\Config::get('super_admin')) {
            $this -> error('超级管理员不支持此操作！');
        }

        $obj -> group() -> detach(true);
        $obj -> delete();
        $this -> success('操作成功！');
    }

    // 分配角色
    public function group()
    {

        $obj = \app\ebcms\model\Manager::get(input('id'));
        if ($obj['email'] == \think\Config::get('super_admin')) {
            $this -> error('超级管理员不支持此操作！');
        }

        if (request()->isGet()) {
            return \ebcms\Form::fetch();
        } elseif (request()->isPost()) {
            // 移除老分组
            \think\Db::name('auth_access')->where(array('uid' => array('eq', input('id'))))->delete();
            // 重组新分组
            $group_ids = input('group_ids/a');
            if ($group_ids) {
                $data = array();
                foreach ($group_ids as $key => $value) {
                    $data[] = array(
                        'uid' => input('id'),
                        'group_id' => $value,
                    );
                }
                if (false !== \think\Db::name('auth_access')->insertAll($data)) {
                    $this->success('角色分配成功！');
                } else {
                    $this->error(\think\Db::name('auth_access')->getDbError());
                }
            } else {
                $this->success('角色分配成功！');
            }
        }
    }

    // 重置密码
    public function password()
    {

        $obj = \app\ebcms\model\Manager::get(input('id'));
        if ($obj['email'] == \think\Config::get('super_admin')) {
            $this -> error('超级管理员不支持此操作！');
        }

        if (request()->isGet()) {
            return \ebcms\Form::fetch();
        } elseif (request()->isPost()) {
            if (false !== $obj -> validate('manager.password') -> save(['password'=>input('password')])) {
                $this->success('密码已经重置！请尽快修改密码！谢谢');
            } else {
                $this->error($obj -> getError());
            }
        }
    }

    // 显示用户信息
    public function info()
    {
        return $this->fetch();
    }

}